package br.com.novagalvao.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import br.com.novagalvao.beans.Categoria;
import br.com.novagalvao.beans.Categoria;
import br.com.novagalvao.services.CategoriaService;

@Controller
@RequestMapping(value = "/categoria")
public class CategoriaController {

	private static final String CONTEXT_PAGE = "pages";
	@Autowired
	private CategoriaService categoriaService;
		
	@RequestMapping(value = "/novo")
	public String novo() {
		return CONTEXT_PAGE+"/categoria/categoria";
	}

	@RequestMapping(value = "/salvar", method = RequestMethod.POST)
	public String salvar(Categoria categoria){
		categoriaService.salvar(categoria);
		return "redirect:/categoria/listar";
	}
	
	@RequestMapping(value = "/listar")
	public ModelAndView listar(){
		
		ModelAndView mav = new ModelAndView(CONTEXT_PAGE+"/categoria/lista");
		List<Categoria> categorias = categoriaService.listar();
		
		mav.addObject(categorias);
		
		return mav;
	}
	
	@RequestMapping(value = "/toEdit/{id}", method = RequestMethod.GET)
	public ModelAndView toEdit(@PathVariable int id){
		
		ModelAndView mav = new ModelAndView(CONTEXT_PAGE+"/categoria/categoria");
		Categoria categoria = categoriaService.find(id);
		
		mav.addObject("categoria", categoria);
		
		return mav;
	}
		
	@RequestMapping(value = "/excluir/{id}", method = RequestMethod.GET)
	public String excluir(@PathVariable int id){
		categoriaService.excluir(id);
		return "redirect:/categoria/listar";
	}
}
